package analysis;

import common.AnalysisWare;
import common.Constants;
import common.DataAnalysis;
import util.FileUtil;
import util.mysql.MysqlUtil;
import util.remote.RemoteConnectResult;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by J.Chen on 2017/5/27.
 */
@AnalysisWare(confCode = "ACT_2016_12_FOREAT_MIST", desc = "迷雾森林")
public class ForeatMistAnalysis extends DataAnalysis {

    public ForeatMistAnalysis(RemoteConnectResult result, String confCode) {
        super(result, confCode);
    }

    @Override
    public void execute() {
        String sql = MysqlUtil.getLogActSql("user_id,COUNT(1) `num`", "user_id", actConf, "doClick", 1, null, null, null);
        List<Map<String, String>> list = result.getMysqlUtil().selectList(sql);
        FileUtil fileUtil = new FileUtil(Constants.getActOutputDir(result.getRemoteConf(), actConf.getConfCode(), "付费玩家", actConf.getTimeStartStr()));
        fileUtil.writeMysqlList(list, "|", "user_id", "num");
        fileUtil.close();
        FileUtil fileUtil1 = new FileUtil(Constants.getActOutputDir(result.getRemoteConf(), actConf.getConfCode(), "领取玩家", actConf.getTimeStartStr()));
        Set<String> set = result.getRedisUtil().zrevrange(getLogAwardsKey(), 0, -1);
        Map<String, Integer> map = new HashMap<>();
        for (String s : set) {
            String[] ss = s.split(":", 2);
            if (map.containsKey(ss[1])) {
                map.put(ss[1], map.get(ss[1]) + 1);
            } else {
                map.put(ss[1], 1);
            }
        }
        for (Map.Entry<String, Integer> en : map.entrySet()) {
            fileUtil1.writeln("|", "" + en.getKey(), "" + en.getValue());
        }
        fileUtil1.close();

    }

    private String getLogAwardsKey() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return String.join(":", "zAct", "foreat", "mist", "logAwards", sdf.format(actConf.getTimeStart()));
    }
}
